/* Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.springboot;

import lombok.extern.slf4j.Slf4j;
import org.snaker.engine.DBAccess;
import org.snaker.engine.access.AbstractDBAccess;
import org.snaker.engine.entity.Process;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Spring jdbc方式的数据库访问操作
 * @author yuqs
 * @since 1.0
 */
@Slf4j
public class SpringJdbcAccess extends AbstractDBAccess implements DBAccess {
	private final JdbcTemplate template;

	public SpringJdbcAccess(JdbcTemplate template) {
		this.template = template;
	}

	private void processBlobBytes(Process process){
		if(Objects.nonNull(process.getBytes())) {
			template.update(PROCESS_UPDATE_BLOB, process.getBytes(), process.getId());
		}
	}
	public void saveProcess(final Process process) {
		super.saveProcess(process);
		processBlobBytes(process);
	}

	public void updateProcess(final Process process) {
		super.updateProcess(process);
		processBlobBytes(process);
	}

	public boolean isORM() {
		return false;
	}

	public void saveOrUpdate(Map<String, Object> map) {
		String sql = (String)map.get(KEY_SQL);
		Object[] args = (Object[])map.get(KEY_ARGS);
		int[] type = (int[])map.get(KEY_TYPE);
		if(log.isDebugEnabled()) {
            log.debug("增删改数据(Spring托管事务)=\n{}", sql);
		}
		template.update(sql, args, type);
	}

	public Integer getLatestProcessVersion(String name) {
		String where = " where name = ?";
        return template.queryForObject(QUERY_VERSION + where, new Object[]{name }, Integer.class);
	}

	public <T> T queryObject(Class<T> clazz, String sql, Object... args) {
		if(log.isDebugEnabled()) {
            log.debug("查询单条数据=\n{}", sql);
		}
		try {
			return template.queryForObject(sql, args, clazz);
		} catch(Exception e) {
            log.error("查询单条数据=\n{}", e.getMessage());
			return null;
		}
	}

	public <T> List<T> queryList(Class<T> clazz, String sql, Object... args) {
		if(log.isDebugEnabled()) {
            log.debug("查询多条数据=\n{}", sql);
		}
		return template.queryForList(sql, args, clazz);
	}

    public Object queryCount(String sql, Object... args) {
        return template.queryForObject(sql, args, Long.class);
    }



    protected Connection getConnection() throws SQLException {
        return Objects.requireNonNull(template.getDataSource()).getConnection();
    }
}
